package edu.ues21.tcs.ecommerce2011.mvc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import edu.ues21.tcs.ecommerce2011.mvc.model.Articulo;
import edu.ues21.tcs.ecommerce2011.mvc.model.ArticuloPrecio;
import edu.ues21.tcs.ecommerce2011.mvc.model.DetallePedido;
import edu.ues21.tcs.ecommerce2011.mvc.model.ListaPrecio;

/**
 *
 * @author eandre fmantaras juanini
 */
@Transactional
public class DetalleCarritoDAOImpl implements DetalleCarritoDAO {

    ResultSet rs = null;
    Connection c = null;
    Statement stmt = null;
    private ApplicationContext ctx;
    private ListaPrecioDAO lpdao;
    private ArticuloPrecioDAO apdao;

    public DetalleCarritoDAOImpl() {
    }

    public boolean setDetalleCarrito(Integer idCarrito, Integer cantidad, Integer idArticulo) {


        try {
            quitarDetalleCarrito(idCarrito, idArticulo);
            c = MySqlDb.getConnection();
            stmt = c.createStatement();
            stmt.executeUpdate("INSERT INTO DetalleCarrito (`cantidad`,`articulo`,`carrito`) VALUES ('" + cantidad + "','" + idArticulo + "','" + idCarrito + "')");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                c.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }

        return false;
    }

    public List<DetallePedido> getDetalleCarrito(Integer idCarrito) {

        ctx = new ClassPathXmlApplicationContext("BeanLocations.xml");
        lpdao = (ListaPrecioDAO) ctx.getBean("listaprecioDAO");
        apdao = (ArticuloPrecioDAO) ctx.getBean("articuloprecioDAO");


        List<DetallePedido> result = new ArrayList<DetallePedido>();


        ListaPrecio lis = lpdao.getListaPrecioHoy();
        ResultSet rs = null;
        try {
            c = MySqlDb.getConnection();
            stmt = c.createStatement();
            rs = stmt.executeQuery("SELECT * FROM DetalleCarrito WHERE carrito=" + idCarrito);

            while (rs.next()) {
                DetallePedido dp = new DetallePedido();
                Articulo articulo = new Articulo();
                articulo.setIdarticulo(rs.getInt("articulo"));
                ArticuloPrecio ap = apdao.getArticuloPrecio(articulo.getIdarticulo(), lis.getIdListaPrecio());
                dp.setArticulo(ap);
                dp.setCantidad(rs.getInt("cantidad"));
                result.add(dp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                stmt.close();
                c.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        return result;
    }

    public void deleteDetalleCarrito(Integer idCarrito) {

        try {
            c = MySqlDb.getConnection();
            stmt = c.createStatement();
            stmt.execute("DELETE FROM DetalleCarrito WHERE carrito=" + idCarrito);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                c.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public void quitarDetalleCarrito(Integer idCarrito, Integer idArticulo) {

        try {
            c = MySqlDb.getConnection();
            stmt = c.createStatement();
            stmt.execute("DELETE FROM DetalleCarrito WHERE carrito=" + idCarrito + " AND articulo=" + idArticulo);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                c.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
